import produce from 'immer';

function compare(a, b) {
  if (a.region_code < b.region_code)
    return -1;
  if (a.region_code > b.region_code)
    return 1;
  return 0;
}


export default {
  namespace: 'statData',
  state: {
    dataType: '',
    yearList: [],
    currentYear: '2017',
    currentDataLevel: '630',
    currentRegions: ['310', '320', '330', '340'],
    records: [],
    renderRecords: [],
    mapLinked: true,
    selectedRecord: undefined
  },
  reducers: {
    save(state, { payload }) {
      produce(state, draft => {
        draft.dataType = payload.dataType;
        draft.yearList = payload.yearList;
        draft.currentYear = payload.currentYear;
        draft.currentDataLevel = payload.currentDataLevel;
        draft.records = payload.records;
        draft.renderRecords = draft.records.filter(item => item.region_level.toString().indexOf(payload.currentDataLevel) !== -1);
      })
    },
    saveMapLinked(state, { payload }) {
      produce(state, draft => {
        draft.mapLinked = payload;
      })
    },
    changeRenderRecords(state, { payload }) {
      const { currentYear, currentDataLevel, currentRegions } = payload;
      produce(state, draft => {
        draft.currentYear = currentYear;
        draft.currentDataLevel = currentDataLevel;
        draft.currentRegions = currentRegions;
        if (draft.mapLinked) {
          // draft.renderRecords = draft.records.filter(item => {
          // item.region_level.toString().indexOf(currentDataLevel) !== -1
          // &&
          // item.stat_year.toString().indexOf(currentYear) !== -1 
          // &&
          // currentRegions.indexOf(item.region_code.toString().substring(0, 3)) !== -1
          // });
          draft.renderRecords = draft.records.filter(item =>
            item.region_level.toString().indexOf(currentDataLevel) !== -1 &&
            // item.stat_year.toString().indexOf(currentYear) !== -1 &&
            currentRegions.indexOf(item.region_code.toString().substring(0, 3)) !== -1
          ).sort(compare);
        }
      })
    },
    changeSelected(state, { payload }) {
      const { region_code, stat_year } = payload;
      produce(state, draft => {
        draft.selectedRecord = payload;
        draft.renderRecords.forEach(item => {
          item.selected = item.region_code === region_code && item.stat_year === stat_year;
        })
        draft.records.forEach(item => {
          item.selected = item.region_code === region_code && item.stat_year === stat_year;
        });
      })
    },
  },
  effects: {
    *init(_, { put, select }) {
      console.log('init data');
      const { currentYear, currentDataLevel, currentRegions } = yield select(state => state.statData);
      yield put({ type: 'changeRenderRecords', payload: { currentYear, currentDataLevel, currentRegions } });
    },
    *setData({ payload }, { put, select }) {
      const { currentYear, currentDataLevel, currentRegions } = yield select(state => state.statData);
      yield put({ type: 'save', payload });
      yield put({ type: 'changeRenderRecords', payload: { currentYear, currentDataLevel, currentRegions } });
    },
    *setMapLinked({ payload }, { put }) {
      yield put({ type: 'saveMapLinked', payload });
    },
    *setDataLevel({ payload: { level } }, { put, select }) {
      const { currentYear, currentDataLevel, currentRegions } = yield select(state => state.statData);
      if (currentDataLevel !== level) {
        yield put({ type: 'changeRenderRecords', payload: { currentYear, currentDataLevel: level, currentRegions } });
      }
    },
    *setRegions({ payload: { regions } }, { put, select }) {
      const { currentYear, currentDataLevel } = yield select(state => state.statData);
      yield put({ type: 'changeRenderRecords', payload: { currentYear, currentDataLevel, currentRegions: regions } });
    },
    *setSelectedYear({ payload: { year } }, { put, select }) {
      const { currentYear, currentDataLevel, currentRegions } = yield select(state => state.statData);
      if (currentYear !== year) {
        yield put({ type: 'changeRenderRecords', payload: { currentYear: year, currentDataLevel, currentRegions } })
      }
    },
    *setSelected({ payload }, { put }) {
      yield put({ type: 'changeSelected', payload })
    }
  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname }) => {

      })
    }
  }
}